package com.yu.crm.controller;


import com.yu.crm.domain.pojo.Img;
import com.yu.crm.domain.request.ImgRequest;
import com.yu.crm.service.impl.ImgService;
import com.yu.crm.utils.FileBase64Util;
import com.yxzz.common.exception.GeneralException;
import com.yxzz.common.helper.DataHelpers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

import static com.yu.crm.utils.FileBase64Util.fileToBase64;

/**
 * <p>
 * 图片表 前端控制器
 * </p>
 *
 * @author zds
 * @since 2021-03-22
 */
@Api(tags = "图片管理")
@RestController
@RequestMapping("/img")
public class ImgController {

    @Autowired
    private ImgService imgService;

    /**
     * 上传图片(通过FastDFS管理文件存储路径)
     * @param imgRequest
     * @return
     * @throws Exception
     */
    @PostMapping("/upload")
    @ApiOperation(value = "上传")
    public List<Img> upload(@RequestBody ImgRequest imgRequest)  {
        if (DataHelpers.isEmpty(imgRequest.getPhoto())) {
            throw new GeneralException("文件请求参为空");
        }
        if (DataHelpers.isEmpty(imgRequest.getId())) {
            throw new GeneralException("身份证号请求参为空");
        }
        MultipartFile multipartFile = FileBase64Util.base64ToMultipartFile(imgRequest.getPhoto());
        System.out.println("file:"+multipartFile);
        return imgService.upload(multipartFile, imgRequest.getId());
    }

    /**
     * 上传图片(通过FastDFS管理文件存储路径)
     * @param
     * @return
     * @throws Exception
     */
    @GetMapping("/upload2")
    @ApiOperation(value = "上传2")
    public List<Img> upload2()  {
//        if (DataHelpers.isEmpty(imgRequest.getPhoto())) {
//            throw new GeneralException("文件请求参为空");
//        }
//        if (DataHelpers.isEmpty(imgRequest.getId())) {
//            throw new GeneralException("身份证号请求参为空");
//        }
        String aaa = "429111199211264545";
        String bbb = FileBase64Util.fileToBase64("C:\\Users\\99608\\Desktop\\test\\a.png");
        return imgService.upload(bbb, aaa);
    }


}

